{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "## 数据的读取加载\n",
        "    pandas中的dtype类型\n",
        "        int\n",
        "        float\n",
        "        datatime\n",
        "        bool\n",
        "        "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "import pandas\n",
        "food_info \u003d pandas.read_csv(\"food_info.csv\")\n",
        "\n",
        "# 数据的读取\n",
        "# 查看pands中的数据的前n行 dataframe.head(n)\n",
        "food_info.head(10) # 输出前n行\n",
        "food_info.columns  #查看数据中的列名\n",
        "food_info.shape    # 查看矩阵数据的维度规模\n",
        "food_info.ndim     # 查看数据的维度\n",
        "food_info.tail(3)  # 查看数据的最后n行超过数据范围会报错\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "## 数据的索引和计算"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "food_info.loc[0]  # 取出第n行数据\n",
        "food_info.loc[2]  #\n",
        "food_info.loc[1:6] # 取出指定行的数据\n",
        "ndb_ccol \u003d food_info[\"NDB_No\"]\n",
        "print(ndb_ccol)   # 通过列名取数据\n",
        "\n",
        "# 通过将列名放置在列表中,来取多列的数据\n",
        "cols_name \u003d [\"Zinc_(mg)\", \"Copper_(mg)\"]\n",
        "cols_2 \u003d food_info[cols_name]  # 取出多行\n",
        "# cols_2\n",
        "\n",
        "# 加减乘除\n",
        "div1000 \u003d food_info[\"Iron_(mg)\"]/1000  # 列除法\n",
        "print(div1000)\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "# 将列计算后的结果作为新的的属性加入到原矩阵中 \n",
        "# 创建新的属性列, 而后加入属性列\n",
        "#water_energy \u003d food_info[\"Water_(g)\"] * food_info[\"Energ_Kcal\"]\n",
        "water_energy \u003d food_info[\"Water_(g)\"] * food_info[\"Energ_Kcal\"]\n",
        "iron_grams \u003d food_info[\"Iron_(mg)\"] / 1000\n",
        "food_info[\"Iron_(g)\"] \u003d iron_grams  # 将新的计算列加入到原矩阵中\n",
        "print(food_info.shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "### 矩阵的元素列中的最大值 .max 最小值.min 均值 数据排序"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0       0.794900\n",
            "1       0.794900\n",
            "2       0.971175\n",
            "3       0.391353\n",
            "4       0.411308\n",
            "5       0.370288\n",
            "6       0.332594\n",
            "7       0.416851\n",
            "8       0.450111\n",
            "9       0.429047\n",
            "10      0.436807\n",
            "11      0.108647\n",
            "12      0.107539\n",
            "13      0.079823\n",
            "14      0.089800\n",
            "15      0.079823\n",
            "16      0.379157\n",
            "17      0.395787\n",
            "18      0.292683\n",
            "19      0.431264\n",
            "20      0.516630\n",
            "21      0.394678\n",
            "22      0.457871\n",
            "23      0.362528\n",
            "24      0.413525\n",
            "25      0.332594\n",
            "26      0.352550\n",
            "27      0.281596\n",
            "28      0.333703\n",
            "29      0.407982\n",
            "          ...   \n",
            "8588    0.431264\n",
            "8589    0.100887\n",
            "8590    0.075388\n",
            "8591    0.515521\n",
            "8592    0.444568\n",
            "8593    0.475610\n",
            "8594    0.080931\n",
            "8595    0.198448\n",
            "8596    0.417960\n",
            "8597    0.310421\n",
            "8598    0.762749\n",
            "8599    0.980044\n",
            "8600    0.309313\n",
            "8601    0.284922\n",
            "8602    0.353659\n",
            "8603    0.394678\n",
            "8604    0.068736\n",
            "8605    0.198448\n",
            "8606    0.200665\n",
            "8607    0.318182\n",
            "8608    0.404656\n",
            "8609    0.389135\n",
            "8610    0.388027\n",
            "8611    0.410200\n",
            "8612    0.080931\n",
            "8613    0.338137\n",
            "8614    0.123060\n",
            "8615    0.298226\n",
            "8616    0.099778\n",
            "8617    0.098670\n",
            "Name: Energ_Kcal, Length: 8618, dtype: float64\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "[\u0027Categorical\u0027,\n",
              " \u0027CategoricalIndex\u0027,\n",
              " \u0027DataFrame\u0027,\n",
              " \u0027DateOffset\u0027,\n",
              " \u0027DatetimeIndex\u0027,\n",
              " \u0027ExcelFile\u0027,\n",
              " \u0027ExcelWriter\u0027,\n",
              " \u0027Expr\u0027,\n",
              " \u0027Float64Index\u0027,\n",
              " \u0027Grouper\u0027,\n",
              " \u0027HDFStore\u0027,\n",
              " \u0027Index\u0027,\n",
              " \u0027IndexSlice\u0027,\n",
              " \u0027Int64Index\u0027,\n",
              " \u0027Interval\u0027,\n",
              " \u0027IntervalIndex\u0027,\n",
              " \u0027MultiIndex\u0027,\n",
              " \u0027NaT\u0027,\n",
              " \u0027Panel\u0027,\n",
              " \u0027Period\u0027,\n",
              " \u0027PeriodIndex\u0027,\n",
              " \u0027RangeIndex\u0027,\n",
              " \u0027Series\u0027,\n",
              " \u0027SparseArray\u0027,\n",
              " \u0027SparseDataFrame\u0027,\n",
              " \u0027SparseSeries\u0027,\n",
              " \u0027Term\u0027,\n",
              " \u0027TimeGrouper\u0027,\n",
              " \u0027Timedelta\u0027,\n",
              " \u0027TimedeltaIndex\u0027,\n",
              " \u0027Timestamp\u0027,\n",
              " \u0027UInt64Index\u0027,\n",
              " \u0027WidePanel\u0027,\n",
              " \u0027_DeprecatedModule\u0027,\n",
              " \u0027__builtins__\u0027,\n",
              " \u0027__cached__\u0027,\n",
              " \u0027__doc__\u0027,\n",
              " \u0027__docformat__\u0027,\n",
              " \u0027__file__\u0027,\n",
              " \u0027__loader__\u0027,\n",
              " \u0027__name__\u0027,\n",
              " \u0027__package__\u0027,\n",
              " \u0027__path__\u0027,\n",
              " \u0027__spec__\u0027,\n",
              " \u0027__version__\u0027,\n",
              " \u0027_hashtable\u0027,\n",
              " \u0027_lib\u0027,\n",
              " \u0027_libs\u0027,\n",
              " \u0027_np_version_under1p10\u0027,\n",
              " \u0027_np_version_under1p11\u0027,\n",
              " \u0027_np_version_under1p12\u0027,\n",
              " \u0027_np_version_under1p13\u0027,\n",
              " \u0027_np_version_under1p14\u0027,\n",
              " \u0027_np_version_under1p15\u0027,\n",
              " \u0027_tslib\u0027,\n",
              " \u0027_version\u0027,\n",
              " \u0027api\u0027,\n",
              " \u0027bdate_range\u0027,\n",
              " \u0027compat\u0027,\n",
              " \u0027concat\u0027,\n",
              " \u0027core\u0027,\n",
              " \u0027crosstab\u0027,\n",
              " \u0027cut\u0027,\n",
              " \u0027date_range\u0027,\n",
              " \u0027datetime\u0027,\n",
              " \u0027datetools\u0027,\n",
              " \u0027describe_option\u0027,\n",
              " \u0027errors\u0027,\n",
              " \u0027eval\u0027,\n",
              " \u0027factorize\u0027,\n",
              " \u0027get_dummies\u0027,\n",
              " \u0027get_option\u0027,\n",
              " \u0027get_store\u0027,\n",
              " \u0027groupby\u0027,\n",
              " \u0027infer_freq\u0027,\n",
              " \u0027interval_range\u0027,\n",
              " \u0027io\u0027,\n",
              " \u0027isna\u0027,\n",
              " \u0027isnull\u0027,\n",
              " \u0027json\u0027,\n",
              " \u0027lib\u0027,\n",
              " \u0027lreshape\u0027,\n",
              " \u0027match\u0027,\n",
              " \u0027melt\u0027,\n",
              " \u0027merge\u0027,\n",
              " \u0027merge_asof\u0027,\n",
              " \u0027merge_ordered\u0027,\n",
              " \u0027notna\u0027,\n",
              " \u0027notnull\u0027,\n",
              " \u0027np\u0027,\n",
              " \u0027offsets\u0027,\n",
              " \u0027option_context\u0027,\n",
              " \u0027options\u0027,\n",
              " \u0027pandas\u0027,\n",
              " \u0027parser\u0027,\n",
              " \u0027period_range\u0027,\n",
              " \u0027pivot\u0027,\n",
              " \u0027pivot_table\u0027,\n",
              " \u0027plot_params\u0027,\n",
              " \u0027plotting\u0027,\n",
              " \u0027pnow\u0027,\n",
              " \u0027qcut\u0027,\n",
              " \u0027read_clipboard\u0027,\n",
              " \u0027read_csv\u0027,\n",
              " \u0027read_excel\u0027,\n",
              " \u0027read_feather\u0027,\n",
              " \u0027read_fwf\u0027,\n",
              " \u0027read_gbq\u0027,\n",
              " \u0027read_hdf\u0027,\n",
              " \u0027read_html\u0027,\n",
              " \u0027read_json\u0027,\n",
              " \u0027read_msgpack\u0027,\n",
              " \u0027read_parquet\u0027,\n",
              " \u0027read_pickle\u0027,\n",
              " \u0027read_sas\u0027,\n",
              " \u0027read_sql\u0027,\n",
              " \u0027read_sql_query\u0027,\n",
              " \u0027read_sql_table\u0027,\n",
              " \u0027read_stata\u0027,\n",
              " \u0027read_table\u0027,\n",
              " \u0027reset_option\u0027,\n",
              " \u0027scatter_matrix\u0027,\n",
              " \u0027set_eng_float_format\u0027,\n",
              " \u0027set_option\u0027,\n",
              " \u0027show_versions\u0027,\n",
              " \u0027test\u0027,\n",
              " \u0027testing\u0027,\n",
              " \u0027timedelta_range\u0027,\n",
              " \u0027to_datetime\u0027,\n",
              " \u0027to_msgpack\u0027,\n",
              " \u0027to_numeric\u0027,\n",
              " \u0027to_pickle\u0027,\n",
              " \u0027to_timedelta\u0027,\n",
              " \u0027tools\u0027,\n",
              " \u0027tseries\u0027,\n",
              " \u0027tslib\u0027,\n",
              " \u0027unique\u0027,\n",
              " \u0027util\u0027,\n",
              " \u0027value_counts\u0027,\n",
              " \u0027wide_to_long\u0027]"
            ]
          },
          "execution_count": 29,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": "max_col \u003d food_info[\"Energ_Kcal\"].max() # 矩阵中的元素列的最大值\nmax_col\n\n# 归一化 每个元素/最大值\nnormalized_col \u003d food_info[\"Energ_Kcal\"] / max_col # 归一化计算\nprint(normalized_col)\n\n# 数据排序 v.sort_value\nfood_info.sort_values(\"Sodium_(mg)\", inplace\u003dTrue)  # inplace表示排序后的矩阵为新的矩阵\nprint(food_info[\"Sodium_(mg)\"])\n# 升序怕.排序\nfood_info.sort_values(\"Sodium_(mg)\", inplace\u003dTrue, ascending\u003dFalse)  \n# inplace表示排序后的矩阵为新的矩阵\n# ascending 默认从小到大排序 \n"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "## 数据预处理"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}